package com.tinshow.config.utils.http.v42;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HttpsUtil {

    private static final String DEFAULT_CHARSET = "utf-8";

    public static String doPost(HttpsClient httpsClient, String url, Map<String, String> paramHeader, Map<String, String> paramBody) throws Exception {
        return doPost(httpsClient, url, paramHeader, paramBody,DEFAULT_CHARSET);
    }

    public static String doPost(HttpsClient httpsClient, String url, Map<String, String> paramHeader, Map<String, String> paramBody, String charset) throws Exception{
        String result = null;

        HttpPost httpPost = new HttpPost(url);
        setHeader(httpPost,paramHeader);
        setBody(httpPost,paramBody,charset);

        HttpResponse response = httpsClient.execute(httpPost);
        if (response != null){
            HttpEntity entity = response.getEntity();
            if(entity != null){
                result = EntityUtils.toString(entity,charset);
            }
        }
        return result;
    }

    public static String doGet(HttpsClient httpsClient, String url, Map<String,String> paramHeader, Map<String, String> paramBody) throws Exception{

        return doGet(httpsClient, url, paramHeader, paramBody, DEFAULT_CHARSET);
    }

    public static String doGet(HttpsClient httpsClient, String url, Map<String,String> paramHeader, Map<String, String> paramBody, String charset) throws Exception{
        String result = null;

        HttpGet httpGet = new HttpGet(url);
        setHeader(httpGet,paramHeader);

        HttpResponse response = httpsClient.execute(httpGet);
        if (response != null){
            HttpEntity entity = response.getEntity();
            if(entity != null){
                result = EntityUtils.toString(entity,charset);
            }
        }
        return result;
    }

    private static void setHeader(HttpRequestBase requestBase, Map<String, String> paramHeader) {
        if (paramHeader != null){
            Set<String> keySet = paramHeader.keySet();
            for (String key: keySet
                 ) {
                requestBase.setHeader(key, paramHeader.get(key));
            }
        }
    }

    private static void setBody(HttpPost httpPost, Map<String,String> paramBody, String charset) throws Exception{
        if(paramBody != null) {
            List<NameValuePair> list = Arrays.asList();
            Set<String> keySet = paramBody.keySet();
            for (String key: keySet
                 ) {
                list.add(new BasicNameValuePair(key, paramBody.get(key)));
            }
            if(list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
                httpPost.setEntity(entity);
            }
        }
    }
}
